package eteam.aps.controller;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.dto.SalesAuditDto;
import eteam.aps.model.entity.SalesAudit;
import eteam.aps.service.SalesAuditService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * SalesAudit 前端控制器
 *
 * @author ll
 * @since 2023-06-02
 */
@Api(tags = "发货单审核")
@RestController
@RequestMapping("zsa")
public class SalesAuditController {

    @Autowired
    private SalesAuditService salesAuditService;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @PostMapping("/audit")
    @ApiOperation("审核发货单")
    @ApiImplicitParam(name = "SalesAudit",value = "发货单审核")
    public int updateSalesAudit(@RequestBody SalesAudit salesAudit) throws Exception {
        if (Helper.isEmpty(salesAudit.getfOrderNum())) throw new BusinessException("单号不能为空！");
        if (Helper.isEmpty(salesAudit.getfSalesOrder())) throw new BusinessException("发货单号不能为空！");
        if (salesAudit.getfDverifySysTime() == null) throw new BusinessException("审核具体数据不能为空！");
        if (salesAudit.getfDverifyDate() == null) throw new BusinessException("审核时间不能为空！");
        VerifyTokenDto user = loginUserRedisService.getUser();
        LocalDateTime now = LocalDateTime.now();
        salesAudit.setfDverifySysTime(now);
        salesAudit.setfDverifyDate(now.toLocalDate());
        salesAudit.setfLastModifyUserId(user.getFuserid());
        salesAudit.setfLastModifyTime(now);
        return salesAuditService.updateSalesAudit(salesAudit);
    }

    @PostMapping("/selectAll")
    @ApiOperation("分页查询SalesAudit")
    @ApiImplicitParam(name = "SalesAuditDto",value = "发货单审核查询条件")
    public JsonPage<SalesAudit> selectSalesAuditList(@RequestBody SalesAuditDto salesAuditDto) {
        return salesAuditService.selectSalesAuditList(salesAuditDto);
    }

}
